home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / newmake.arc / FILE.C < prev    next >
Text File  |  1986-09-21  |  3KB  |  142 lines

  1. #include <stdio.h>
  2. #include "make.h"
  3.  
  4.  
  5. /*
  6.  * Return file-node for 'fname'.
  7.  * If it doesn't exist, then create one.
  8.  */
  9. FILENODE *filenode(fname)
  10. char *fname;
  11. {
  12.     FILENODE *f, *afnode(), *gfile();
  13.  
  14.     if((f = gfile(fname)) == NULL)
  15.         f = afnode(fname);
  16.     return f;
  17. }
  18.  
  19.  
  20. /*
  21.  * Add a dependency to the node 'fnd'.
  22.  * 'fnd' will depend on 'fname'.
  23.  */
  24. addfile(fnd, fname)
  25. FILENODE *fnd;
  26. char *fname;
  27. {
  28.     NODE *n;
  29.     FILENODE *f;
  30.  
  31.     if(fnd == NULL)         /* punt if no root file */
  32.     {
  33.         fprintf(stderr, "No current root, can't add dependency '%s'\n",
  34. fname);
  35.         return;
  36.     }
  37.  
  38.     f = filenode(fname);
  39.     if((n = (NODE *)malloc(sizeof(NODE))) == NULL) allerr();
  40.     n->nnext = fnd->fnode;
  41.     fnd->fnode = n;
  42.     n->nfile = f;
  43. }
  44.  
  45.  
  46. /*
  47.  * Add a line of method-text to the node 'fnode'.
  48.  */
  49. addmeth(fnode, methtext)
  50. FILENODE *fnode;
  51. char *methtext;
  52. {
  53.     int len;
  54.     char *new;
  55.  
  56.     if(fnode == NULL || methtext == NULL) return;
  57.  
  58.     len = strlen(methtext) + 2;
  59.     if(fnode->fmake == NULL)
  60.     {
  61.         if((fnode->fmake = (char *)malloc(1)) == NULL) allerr();
  62.         *(fnode->fmake) = 0;
  63.     }
  64.     len += strlen(fnode->fmake);
  65.  
  66. /* Lattice C doesn't have 'realloc()', so this kludges around it: */
  67.     if((new = (char *)malloc(len)) == NULL) allerr();
  68.     strcpy(new, fnode->fmake);
  69.     free(fnode->fmake);
  70.     fnode->fmake = new;
  71.  
  72.     strcat(fnode->fmake, methtext);
  73.     len = strlen(fnode->fmake);
  74.     if(len && fnode->fmake[len - 1] != '\n')
  75.         strcat(fnode->fmake, "\n");
  76. }
  77.  
  78.  
  79. /*
  80.  * Get a filenode for the file called 'fn'.
  81.  * Returns NULL if the node doesn't exist.
  82.  */
  83. FILENODE *gfile(fn)
  84. char *fn;
  85. {
  86.     FILENODE *f;
  87.  
  88.     for(f = froot; f != NULL; f = f->fnext)
  89.         if(!strcmp(fn, f->fname)) return f;
  90.     return NULL;
  91. }
  92.  
  93.  
  94. /*
  95.  * Alloc space for a new file node.
  96.  */
  97. FILENODE *afnode(name)
  98. char *name;
  99. {
  100.     FILENODE *f;
  101.  
  102.     for(f=froot; f; f=f->fnext)
  103.         if(!strcmp(name, f->fname)) return f;
  104.  
  105.     if((f = (FILENODE *)malloc(sizeof(FILENODE))) == NULL) allerr();
  106.     if((f->fname = (char *)malloc(strlen(name)+1)) == NULL) allerr();
  107.     strcpy(f->fname, name);
  108.     f->fmake = NULL;
  109.     f->fnode = NULL;
  110.     f->fdate = NULL;
  111.     f->fflag = 0;
  112.  
  113.     f->fnext = froot;
  114.     froot = f;
  115.     return f;
  116. }
  117.  
  118.  
  119. /*
  120.  * Print dependency tree.
  121.  */
  122. prtree()
  123. {
  124.     FILENODE *f;
  125.     NODE *n;
  126.  
  127.     for(f = froot; f != NULL; f = f->fnext)
  128.     {
  129.         printf("%s%s%s (%u, %u)\n",
  130.             f->fname,
  131.             (f->fflag & ROOTP) ? " (root)" : "",
  132.             (f->fflag & REBUILT) ? " (rebuilt)" : "",
  133.             (f->fdate != NULL) ? (f->fdate)->ds_high : 0,
  134.             (f->fdate != NULL) ? (f->fdate)->ds_low : 0);
  135.         if(f->fmake != NULL)
  136.             printf("%s", f->fmake);
  137.         for(n = f->fnode; n != NULL; n = n->nnext)
  138.             printf("\t%s\n", (n->nfile)->fname);
  139.         puts("");
  140.     }
  141. }
  142.